import { createRouter, createWebHistory } from 'vue-router';
import User from '@/views/user/BlogMsg.vue'
import Home1 from '@/components/HomeV.vue'
import ARegister from "@/sign/aRegister.vue";
import ALogin from "@/sign/aLogin.vue";
import Index from "@/views/user/MyBolg.vue"
import BlogDetail from "@/views/user/BlogDetail.vue";
import CreatePost from "@/views/user/CreatePost.vue";
import PostList from "@/views/user/PostList.vue";
import EditPost from '@/views/user/editPost.vue';
import Admin from "@/views/admin/adminBlog.vue"
import EditUser from "@/views/admin/editUser.vue";
import userIndex from "@/views/admin/UserIndex.vue";
import adminPost from "@/views/admin/adminPost.vue";
import adminRemarks from "@/views/admin/RemarkList.vue"
import MyRemarks from "@/views/user/myRemarks.vue";
import myLove from "@/views/user/myLove.vue";
const routes = [

    {
        path: '/',
        name: 'home1',
        component: Home1
    },
    {
        path: "/edit/:id",
        name: "EditPost",
        component: EditPost
    },
    {
        path: "/editUser/:id",
        name: "EditUser",
        component: EditUser
    },
    {
        path: '/blog/:id',
        name: 'BlogDetail',
        component: BlogDetail,
    }, {
        path:'/create',
        name:'create',
        component:CreatePost,
    },
    {
      path: '/user',
      name: 'user',
      component: User,
        children:[
            {
                path: 'index',
                name: 'index',
                component: Index,
            },
            {
                path: 'postList',
                name: 'postList',
                component: PostList,
            },
            {
                path: 'myRemarks',
                name: 'myRemarks',
                component: MyRemarks,
            },
            {
                path: 'myLove',
                name: 'myLove',
                component: myLove,
            },
        ]

    },

    {
        path: '/login',
        name: 'login',
        component: ALogin,
    },
    {
        path: '/register',
        name: 'register',
        component: ARegister,
    },
    {
        path: '/admin',
        name: 'admin',
        component: Admin,
        children:[

    {
        path: 'userIndex',
        name: 'userIndex',
        component: userIndex,
    },
    {
        path: 'adminPosts',
        name: 'adminPosts',
        component: adminPost,
    },
    {
        path: 'adminRemarks',
        name: 'adminRemarks',
        component:adminRemarks,
     },

]
    },


]
const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const user = localStorage.getItem("user");
    if (to.path === '/login' || to.path === '/register'|| to.path === '/') {
        next();
    } else if (user === null) {
        next('/login');
    } else {
        next();
    }
});

router.afterEach((to, from) => {
    console.log(`现在从${from.path} 跳到 ${to.path}`);
});

export default router;